home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / misc < prev    next >
Text File  |  1993-03-12  |  3KB  |  134 lines

  1. /* Miscellaneous machine independent utilities */
  2.  
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include "global.h"
  7. #include "misc.h"
  8.  
  9. /* Case insensitive strcmp */
  10. int stricmp (char *s, char *t)
  11. {
  12.   for ( ; tolower(*s) == tolower(*t); ++s, ++t )
  13.     if ( *s == '\0' )
  14.       return(0);
  15.  
  16.   return (tolower(*s) - tolower(*t));
  17. }
  18.  
  19. /* C.Strnlcmp: compare two strings for n bytes, treating all letters as
  20.  * lower case
  21.  */
  22. int strnlcmp (char *s, char *t, int n)
  23. {
  24.         while ( --n >= 0 )
  25.         {
  26.                 if ( *s == '\0' || *t == '\0' || tolower(*s) != tolower(*t) )
  27.                         return (tolower(*s) - tolower(*t));
  28.                 ++s;
  29.                 ++t;
  30.         }
  31.  
  32.         return (0);
  33. }
  34. /* Convert hex-ascii to integer */
  35. int htoi(char *s)
  36. {
  37.         int i = 0;
  38.         char c;
  39.  
  40.         while((c = *s++) != '\0'){
  41.                 if(c == 'x')
  42.                         continue;       /* allow 0x notation */
  43.                 if('0' <= c && c <= '9')
  44.                         i = (i * 16) + (c - '0');
  45.                 else if('a' <= c && c <= 'f')
  46.                         i = (i * 16) + (c - 'a' + 10);
  47.                 else if('A' <= c && c <= 'F')
  48.                         i = (i * 16) + (c - 'A' + 10);
  49.                 else
  50.                         break;
  51.         }
  52.         return i;
  53. }
  54. /* replace terminating end of line marker(s) with null */
  55. void rip(register char *s)
  56. {
  57.         register char *cp;
  58.  
  59.         if((cp = strchr(s,'\r')) != NULLCHAR)
  60.                 *cp = '\0';
  61.         if((cp = strchr(s,'\n')) != NULLCHAR)
  62.                 *cp = '\0';
  63. }
  64.  
  65. /* Case-insensitive string comparison */
  66. int strnicmp(register char *a, register char *b, register int n)
  67. {
  68.         char a1, b1;
  69.  
  70.         while (n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0')
  71.         {
  72.                 if (a1 == b1)
  73.                         continue;       /* No need to convert */
  74.                 a1 = tolower(a1);
  75.                 b1 = tolower(b1);
  76.                 if (a1 == b1)
  77.                         continue;       /* Now they match */
  78.                 if (a1 > b1)
  79.                         return(1);
  80.                 if (a1 < b1)
  81.                         return(-1);
  82.         }
  83.  
  84.         return(0);
  85. }
  86.  
  87. /* Put a long in host order into a char array in network order */
  88. char *put32(register char *cp, int32 x)
  89. {
  90.         *cp++ = uchar(x >> 24);
  91.         *cp++ = uchar(x >> 16);
  92.         *cp++ = uchar(x >> 8);
  93.         *cp++ = uchar(x);
  94.  
  95.         return(cp);
  96. }
  97.  
  98. /* Put a short in host order into a char array in network order */
  99. char *put16(register char *cp, int16 x)
  100. {
  101.         *cp++ = uchar(x >> 8);
  102.         *cp++ = uchar(x);
  103.  
  104.         return(cp);
  105. }
  106.  
  107. /* Machine independant, alignment insensitive network-to-host short conversion */
  108. int16 get16(register char *cp)
  109. {
  110.         register int16 x;
  111.  
  112.         x = uchar(*cp++);
  113.         x <<= 8;
  114.         x |= uchar(*cp);
  115.  
  116.         return(x);
  117. }
  118.  
  119. /* Machine independant, alignment insensitive network-to-host short conversion */
  120. int32 get32(register char *cp)
  121. {
  122.         register int32 x;
  123.  
  124.         x = uchar(*cp++);
  125.         x <<= 8;
  126.         x |= uchar(*cp++);
  127.         x <<= 8;
  128.         x |= uchar(*cp++);
  129.         x <<= 8;
  130.         x |= uchar(*cp);
  131.  
  132.         return(x);
  133. }
  134.